package com.yykj.im.center.server.util;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeUtil {

    public static LocalDateTime now() {
        return LocalDateTime.now();
    }

    //只能是 yyyy-MM-dd HH:mm:ss 格式
    public static LocalDateTime str2LocalDateTime(String date, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(date, df);
    }

    //传入yyyy-MM-dd 返回这天的开始 yyyy-MM-dd 00:00:00
    public static LocalDateTime day2LocalDateTimeStart(String date) {
        return second2LocalDateTime(date + " 00:00:00");
    }

    //传入yyyy-MM-dd 返回这天的结束 yyyy-MM-dd 23:59:59
    public static LocalDateTime day2LocalDateTimeEnd(String date) {
        return second2LocalDateTime(date + " 23:59:59");
    }

    public static LocalDateTime second2LocalDateTime(String date) {
        return str2LocalDateTime(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static boolean isBeforeNow(LocalDateTime localDateTime) {
        return now().isAfter(localDateTime);
    }

    public static long getBetweenNow(LocalDateTime localDateTime) {
        Duration duration = Duration.between(now(), localDateTime);
        return duration.toMinutes();
    }
    public static long getBetweenAfterNow(LocalDateTime localDateTime) {
        Duration duration = Duration.between(localDateTime, now());
        return duration.toMinutes();
    }
    // >0 表示 1在2之前多少分钟
    public static long getBetween(LocalDateTime localDateTime, LocalDateTime localDateTimez) {
        Duration duration = Duration.between(localDateTime,localDateTimez);
        return duration.toMinutes();
    }

    /**
     * 格式化分钟,小于1小时 返回分,小于24小时返回小时,大于24小时返回多少天多少小时
     * @return
     */
    public static String getTimeDurationMinutesStr(long i){
        if(i < 60){
            return i + "分";
        }
        if(i >= 60 && i < 1440 ){
            String s = i / 60 + "时";
            long l = i % 60;
            if(l > 0){
                s += l + "分";
            }
            return s;
        }
        String s = i / 1440 + "天";
        long l = (i % 1440) / 60;
        if(l > 0){

            s += l + "时";
        }
        return s;
    }

    public static String LocalDateTime2String(LocalDateTime localDateTime){
        return LocalDateTime2String(localDateTime, "yyyy-MM-dd HH:mm:ss");
    }
    public static String LocalDateTime2String(LocalDateTime localDateTime, String format){
        if(localDateTime != null){
            try {
                DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
                return localDateTime.format(df);
            }catch (Exception e){}
        }
        return null;
    }
}